package cn.pn.project.controller;

import cn.pn.project.client.commodity.SkuFeign;
import cn.pn.project.client.schedule.CommoditySessionFeign;
import cn.pn.project.client.schedule.TicketFileFeign;
import cn.pn.project.common.DtoUtil;
import cn.pn.project.pojo.commodity.Sku;
import cn.pn.project.pojo.schedule.Commoditysession;
import cn.pn.project.pojo.schedule.Ticketfile;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ticketfile")
public class TicketFileController {

    @Autowired
    private TicketFileFeign ticketFileFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private CommoditySessionFeign commoditySessionFeign;

    /**
     * 根据场地id查询票档信息
     * @param placeId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/p/getTickFileByPlaceId",method = RequestMethod.POST)
    public Object getTickFileByPlaceId(@RequestParam("placeId") Integer placeId) throws SQLException {
        if (placeId == null) {
            return DtoUtil.returnSuccess("场地id为空", "5007");
        }
        System.out.println(JSON.toJSONString(ticketFileFeign.getTickFileByPlaceId(placeId), true));
        return DtoUtil.returnSuccess("获取成功", ticketFileFeign.getTickFileByPlaceId(placeId));
    }

    /**
     * 查询票档信息
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/p/ticketFileInfo",method = RequestMethod.POST)
    public Object ticketFileInfo(@RequestParam("placeId")Integer placeId,@RequestParam("sessionTime")String sessionTime) throws SQLException {
        System.out.println("dsdsdsdsdsd");
        if (placeId == null) {
            return DtoUtil.returnSuccess("场地id为空", "5008");
        }
        if (sessionTime == null) {
            return DtoUtil.returnSuccess("场次时间为空", "5009");
        }
        Object object = commoditySessionFeign.getCommoditysessionByPlaceIdAndSessionTime(placeId, sessionTime);
        System.out.println("==============================================Object=**" + object);
        //根据场地id和具体的场次时间查询场次id，唯一的一个场次id
        Commoditysession commoditysession = commoditySessionFeign.getCommoditysessionByPlaceIdAndSessionTime(placeId, sessionTime);
        //根据场次id查询sku信息
        List<Sku> list = skuFeign.getSkuByCommdityIdAndSessionId(null, commoditysession.getSessionId());
        //根据场地查询票档信息
        List<Ticketfile> ticketfiles = ticketFileFeign.getTickFileByPlaceId(placeId);
        //定义集合，存放票档信息
        Map<String, String> map = new HashMap<String, String>();
        for (Sku sku : list) {
            for (Ticketfile ticketfile : ticketfiles) {
                if (sku.getTicketFileId() == ticketfile.getTicketfileid()) { //如果sku中的票档id和票档信息中的id相同，进行组合，票档名称为键，票档颜色和价钱为值
                    map.put(ticketfile.getTicketfilename(), ticketfile.getColor() + "," + sku.getPrice() + "," + ticketfile.getAreanum());
                }
            }
        }
        System.out.println("===============================" + JSON.toJSONString(map, true));
        return DtoUtil.returnSuccess("获取成功", map);
        //return JSON.toJSONString(map,true);
        //eturn "ticketFileInfoCallBack("+JSON.toJSONString(map,true)+")";
    }
}
